package com.guxingke.typecho4j.widget.provider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.jfinal.model.Meta;
import com.guxingke.typecho4j.util.Archive;
import com.guxingke.typecho4j.util.DateUtil;
import com.guxingke.typecho4j.util.PageBean;

/**
 * 抽象内容组件
 * 
 * @author guxingke
 * */
public abstract class ContentBaseWidget extends ProviderWidget {

	protected List<Content> getPostsByMetaSlug(String categoryName,
			PageBean pageBean) {
		Long mid = Meta.dao.findMidByMSlug(categoryName);
		return getPostsByMetaId(mid, pageBean);
	}

	private List<Content> getPostsByMetaId(Long mid, PageBean pageBean) {
		return Content.dao.getPostsByMetaId(mid, pageBean);
	}

	protected List<Content> getPosts(PageBean pageBean) {
		return Content.dao.getPosts(pageBean);
	}

	protected List<Content> getRecentPosts() {
		return Content.dao.getRecentPosts(constants.getPageSize());
	}

	protected List<Content> getFriendLinks() {
		return Content.dao.getFriendLinks();
	}

	protected Content getPostById(Long id) {
		return Content.dao.getPostById(id);
	}

	protected List<Content> getPages() {
		return Content.dao.getPages();
	}

	protected Content getPageBySlug(String slug) {
		return Content.dao.getPageBySlug(slug);
	}

	protected Long getSearchPostsTotal(String prop) {
		return Content.dao.getSearchPostsTotal(prop);
	}

	protected Long getAuthorPostsTotal(String prop) {
		return Content.dao.getAuthorPostsTotal(prop);
	}

	protected Long getTagPostsTotal(String prop) {
		return Content.dao.getTagPostsTotal(prop);
	}

	protected Long getCategoryPostsTotal(String prop) {
		return Content.dao.getCategoryPostsTotal(prop);
	}

	protected Long getArchivePostsTotal(String prop) {
		return Content.dao.getArchivePostsTotal(prop);
	}

	protected List<Content> getPostsByQueryInfo(String tag, String prop,
			PageBean pageBean) {
		if ("/author".equals(tag)) {
			Long authorId = Long.parseLong(prop);
			return Content.dao.getPostsByAuthorId(authorId,
					pageBean);
		} else if ("/category".equals(tag) || "/tag".equals(tag)) {
			return getPostsByMetaSlug(prop, pageBean);
		} else if ("/search".equals(tag)) {
			return Content.dao.getPostsByKeyword(prop, pageBean);
		} else if ("/archive".equals(tag)) {
			return Content.dao
					.getPostsByArchiveInfo(prop, pageBean);
		}
		return new ArrayList<Content>();
	}

	protected List<Archive> getArchives() {
		List<Archive> archives = new ArrayList<Archive>();
		Long firstTime = Content.dao.getFirstCreatedTime();

		Long currentTime = DateUtil.getUnixTimestamp();
		archives = buildArchives(firstTime, currentTime);

		Collections.reverse(archives);
		return archives;
	}

	private List<Archive> buildArchives(Long firstTime, Long currentTime) {
		String[] temps = DateUtil.getDateFormatString(firstTime, "Y M")
				.split(" ");
		int year = Integer.parseInt(temps[0]);
		int month = Integer.parseInt(temps[1]);

		List<Archive> archives = new ArrayList<Archive>();

		boolean isEnd = false;
		Long time = DateUtil.getMonthFirstDay(year, month);
		while (!isEnd) {
			Archive archive = new Archive(time);
			archives.add(archive);
			time = DateUtil.getMonthFirstDay(year, ++month);
			if (month > 12) {
				year++;
				month = 1;
				time = DateUtil.getMonthFirstDay(year, month);
			}
			if (time > currentTime) {
				isEnd = true;
			}
		}
		return archives;
	}

	protected List<Content> getAttachments() {
		return Content.dao.getAttachments();
	}
}
